<?php
/**
 * Created by PhpStorm.
 * User: itboye
 * Date: 2016/11/30
 * Time: 10:52
 */
namespace Admin\Controller;
use Think\Controller;
use Shop\Api\OrdersInfoViewApi;
class StatisticsController extends Controller{
    public function index() {
        $map = array();
        $payStatus     = I('paystatus', '');
        $orderStatus   = I('orderstatus', '');
        $startdatetime = I('startdatetime');
        $startdatetime = strtotime($startdatetime);
        $enddatetime   = I('enddatetime');
        $enddatetime   = strtotime($enddatetime);
        if(!empty($startdatetime) && !empty($enddatetime)){
            if($startdatetime === FALSE || $enddatetime === FALSE){
                $params = array('startdatetime' =>$startdatetime, 'enddatetime' =>$enddatetime,'wxaccountid'=>getWxAccountID());
                $map['createtime'] = array( array('EGT', $startdatetime), array('ELT', $enddatetime), 'and');
                $startdatetime = date("Y-m-d h:i:s", $startdatetime);
                $enddatetime   = date("Y-m-d h:i:s", $enddatetime);
            }else{
                $params = array('startdatetime' => $startdatetime, 'enddatetime' =>$enddatetime,'wxaccountid'=>getWxAccountID());
                $map['createtime'] = array( array('EGT', $startdatetime), array('ELT', $enddatetime), 'and');
                $startdatetime = date("Y-m-d h:i:s", $startdatetime);
                $enddatetime   = date("Y-m-d h:i:s", $enddatetime);
            }
        }
        if ($payStatus != '') {
            $map['pay_status']   = $payStatus;
            $params['paystatus'] = $payStatus;
        }
        if ($orderStatus != '') {
            $map['order_status']   = $orderStatus;
            $params['orderstatus'] = $orderStatus;
        }
        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " createtime desc ";
        $field = "goods_amount";

        $result = apiCall(OrdersInfoViewApi::QUERY, array($map, $page, $order, $params));
        $count = apiCall(OrdersInfoViewApi::COUNT, array($map));
        $sum = apiCall(OrdersInfoViewApi::SUM, array($map,$field));
        //
        if ($result['status']) {
            $this -> assign('orderStatus', $orderStatus);
            $this -> assign('payStatus', $payStatus);
            $this -> assign('startdatetime', $startdatetime);
            $this -> assign('enddatetime', $enddatetime);
            $this -> assign('count', $count["info"]);
            $this -> assign('sum',$sum["info"]);
            $this -> assign('list', $result['info']['list']);
            $this -> assign('show', $result['info']['show']);
            $this -> display();
        } else {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error($result['info']);
        }
    }

}